﻿using System;
using System.Collections.Generic;
using System.Linq;
using COFE.Core;

namespace COFE.Data
{
    public static class DBEntryHelper
    {
        public static Dictionary<object, Func<Entry, object>> EntryPropertyGetterDic =
            new Dictionary<object, Func<Entry, object>>()
            {
                { FileSystemInfoProperties.Parent, (e) => e.EntryId == Guid.Empty ? null : 
                     e.ParentId == Guid.Empty ? COFEShared.CurrentUserProfile.ProfileRootDirectory :
                    COFEShared.EntryConstructor.CreateEntry(new DBCachePropertyProvider(e.ParentId), 
                    Core.Profile.EntryConstructionFlags.Default) as IFileSystemInfo},
                { FileSystemInfoProperties.ParentParseName, (e) => e.Parent == null ? "" : 
                    COFEPath.FormatFullParseName(e.Parent.PrimaryVolumeId, e.Parent.ParsePath) },
                { FileSystemInfoProperties.FullParseName, (e) => COFEPath.FormatFullParseName(e.PrimaryVolumeId, e.ParsePath) },
                { FileSystemInfoProperties.SecondaryVolumeKey, (e) => e.SecondaryVolumeId },
                { FileSystemInfoProperties.IsExists, (e) => COFEDBShared.DBOperations.ReadDatabase((context) => context.Entries.Any(dbe => dbe.EntryId == e.EntryId)) },
                { FileSystemInfoProperties.Name, (e) => COFEPath.GetFileName(COFEPath.FormatFullParseName(e.PrimaryVolumeId, e.ParsePath)) },
                { FileSystemInfoProperties.Label, (e) => e.Label },
                { FileSystemInfoProperties.IsFileSystem, (e) => e.IsFileSystem },
                { FileSystemInfoProperties.IsCachable, (e) => true },
                { FileSystemInfoProperties.COFEAttributes, (e) => e.COFEAttributes },
                { FileSystemInfoProperties.FileAttributes, (e) => e.FileAttributes },
                { FileSystemInfoProperties.LastWriteTimeUtc, (e) => e.LastWriteTimeUtc },
                { FileSystemInfoProperties.LastAccessTimeUtc, (e) => e.LastAccessTimeUtc },
                { FileSystemInfoProperties.CreationTimeUtc, (e) => e.CreationTimeUtc },                
                { DatabaseProperties.SupportedProperties, (e) => e.SupportedAttributes },
                { DatabaseProperties.PropertyRefreshTime, (e) => e.PropertyRefreshTime }
            };
        public static Dictionary<object, Action<Entry, object>> EntryPropertySetterDic =
            new Dictionary<object, Action<Entry, object>>()
            {                
                { FileSystemInfoProperties.Label, (e,v) => { e.Label = (string)v; } },
                { FileSystemInfoProperties.COFEAttributes, (e,v) => { e.COFEAttributes = (int)v; } },
                { FileSystemInfoProperties.FileAttributes, (e,v) => { e.FileAttributes = (int)v; } },
                { FileSystemInfoProperties.LastWriteTimeUtc, (e,v) => { e.LastWriteTimeUtc = (DateTime)v; } },
                { FileSystemInfoProperties.LastAccessTimeUtc, (e,v) => { e.LastAccessTimeUtc = (DateTime)v; } },
                { FileSystemInfoProperties.CreationTimeUtc, (e,v) => { e.CreationTimeUtc = (DateTime)v; } },
                { DatabaseProperties.SupportedProperties, (e,v) => { e.SupportedAttributes = (string)v; } }
            };

        public static Dictionary<object, Func<FileEntry, object>> FileEntryPropertyGetterDic =
           new Dictionary<object, Func<FileEntry, object>>()
            {
                { FileInfoProperties.CRC, (e) => e.CRC },
                { DirectoryInfoProperties.IsFileBasedFS, (e) => e.IsFileBasedFS }, 
                { FileInfoProperties.Length, (e) => e.Length }
            };

        public static Dictionary<object, Action<FileEntry, object>> FileEntryPropertySetterDic =
            new Dictionary<object, Action<FileEntry, object>>()
            {
                {  FileInfoProperties.CRC, (e,v) => { e.CRC = (string)v; } },                
                {  DirectoryInfoProperties.IsFileBasedFS, (e,v) => { e.IsFileBasedFS = (bool)v; } }, 
                {  FileInfoProperties.Length, (e,v) => { e.Length = (int)v; } },
            };

        public static Dictionary<object, Func<FolderEntry, object>> FolderEntryPropertyGetterDic =
          new Dictionary<object, Func<FolderEntry, object>>()
            {
               { DirectoryInfoProperties.DirectoryType, (e) => (DirectoryTypeEnum)e.DirectoryType },                
                { DirectoryInfoProperties.HasFiles, (e) => e.FileCount > 0},
                { DirectoryInfoProperties.FileCount, (e) => e.FileCount},
                {  DirectoryInfoProperties.IsFileBasedFS, (e) => false } ,
                { DirectoryInfoProperties.HasSubFolders, (e) => e.SubFolderCount > 0 } ,  
                { DirectoryInfoProperties.SubFolderCount, (e) => e.SubFolderCount},
                { DatabaseProperties.LastListTime, (e) => e.LastListTime }
            };

        public static Dictionary<object, Action<FolderEntry, object>> FolderEntryPropertySetterDic =
            new Dictionary<object, Action<FolderEntry, object>>()
            {
                { DirectoryInfoProperties.DirectoryType, (e,v) => { e.DirectoryType = (int)v; } },
                //{ DirectoryInfoProperties.IsFileBasedFS, (e,v) => { e.IsFileBasedFS = (bool)v; } },
                { DirectoryInfoProperties.FileCount, (e,v) => { e.FileCount = (int)v; } },
                { DirectoryInfoProperties.SubFolderCount, (e,v) => { e.SubFolderCount = (int)v; } },  
                { DatabaseProperties.LastListTime, (e,v) => { e.LastListTime = (DateTime)v; } }
            };
    }
}
